package com.dotto.atos.fragments;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.dotto.atos.R;
import com.dotto.atos.activity.HomeActivity;
import com.dotto.atos.utils.MessageUtil;
import com.dotto.atos.utils.TextUtil;

public class CheckOutFragment extends Fragment {

	public static final String TAG = "CheckOutFragment";
	private View fragmentView;
	private Activity mActivity;
	@SuppressWarnings("unused")
	private CheckOutFragment instance;

	private MessageUtil messageUtil;

	private Button tabLeft;
	private Button tabMiddle;
	private Button tabRight;
	private Button btnCardScanner;
	private EditText inptCardNumber;
	private EditText inptCvv2;
	private EditText inptExDateMM;
	private EditText inptExDateYYYY;
	private EditText inptCardOwnerName;
	private EditText inptAmount;
	private Button btnPay;
	@SuppressWarnings("unused")
	private int selectedTab;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (fragmentView == null) {
			fragmentView = inflater.inflate(R.layout.fragment_checkout,
					container, false);
		} else {
			((ViewGroup) fragmentView.getParent()).removeView(fragmentView);
		}
		mActivity = this.getActivity();
		instance = this;
		messageUtil = new MessageUtil(mActivity);
		initView();
		return fragmentView;
	}

	public void initView() {
		if (fragmentView != null) {
			tabLeft = (Button) fragmentView
					.findViewById(R.id.check_out_tab_left);
			tabMiddle = (Button) fragmentView
					.findViewById(R.id.check_out_tab_middle);
			tabRight = (Button) fragmentView
					.findViewById(R.id.check_out_tab_right);
			btnCardScanner = (Button) fragmentView
					.findViewById(R.id.check_out_btn_card_scanner);
			inptCardNumber = (EditText) fragmentView
					.findViewById(R.id.check_out_car_number);
			inptCvv2 = (EditText) fragmentView
					.findViewById(R.id.check_out_item_inpt_cvv2);
			inptExDateMM = (EditText) fragmentView
					.findViewById(R.id.check_out_item_inpt_mounth);
			inptExDateYYYY = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_year);
			inptCardOwnerName = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_owner_name);
			inptAmount = (EditText) fragmentView
					.findViewById(R.id.check_out_item_inpt_amount);
			btnPay = (Button) fragmentView.findViewById(R.id.check_out_pay_btn);

			inptCardNumber.setText(null);
			inptCvv2.setText(null);
			inptExDateMM.setText(null);
			inptExDateYYYY.setText(null);
			inptCardOwnerName.setText(null);

			inptAmount.setText(OnlinePaymentFragment.amount);
			inptAmount.setFocusable(false);
			inptAmount.setFocusableInTouchMode(false);
			inptAmount.setClickable(false);

			tabLeft.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					selectTab(1);
				}
			});
			tabMiddle.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					selectTab(2);
				}
			});
			tabRight.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					selectTab(3);
				}
			});

			selectTab(1);

			btnPay.setOnClickListener(onClickPay);
			btnCardScanner.setOnClickListener(onClickScan);
		}
	}

	public OnClickListener onClickScan = new OnClickListener() {

		@Override
		public void onClick(View v) {
			HomeActivity.instance.scanCard();
		}
	};

	public void onCardReaded(String card) {
		inptCardNumber.setText(card);
	}

	public OnClickListener onClickPay = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String carnumber = inptCardNumber.getText().toString();
			inptCardNumber.setBackgroundResource(R.drawable.edittext);
			String cvv2 = inptCvv2.getText().toString();
			inptCvv2.setBackgroundResource(R.drawable.edittext);
			String mm = inptExDateMM.getText().toString();
			inptExDateMM.setBackgroundResource(R.drawable.edittext);
			String yyyy = inptExDateYYYY.getText().toString();
			inptExDateYYYY.setBackgroundResource(R.drawable.edittext);
			String ownername = inptCardOwnerName.getText().toString();
			inptCardOwnerName.setBackgroundResource(R.drawable.edittext);
			boolean error = false;
			if (TextUtil.isEmpty(carnumber)
					|| !TextUtil.checkMinChar(carnumber, 16)
					|| !TextUtil.checkMaxChar(carnumber, 16)) {
				error = true;
				inptCardNumber.setBackgroundResource(R.drawable.edittext_error);
			}
			if (TextUtil.isEmpty(cvv2) || !TextUtil.checkMinChar(cvv2, 3)
					|| !TextUtil.checkMaxChar(cvv2, 3)) {
				error = true;
				inptCvv2.setBackgroundResource(R.drawable.edittext_error);
			}
			if (TextUtil.isEmpty(mm) || !TextUtil.checkMinChar(mm, 2)
					|| !TextUtil.checkMaxChar(mm, 2)) {
				error = true;
				inptExDateMM.setBackgroundResource(R.drawable.edittext_error);
			}
			if (TextUtil.isEmpty(yyyy) || !TextUtil.checkMinChar(yyyy, 4)
					|| !TextUtil.checkMaxChar(yyyy, 4)) {
				error = true;
				inptExDateYYYY.setBackgroundResource(R.drawable.edittext_error);
			}
			if (TextUtil.isEmpty(ownername)
					|| !TextUtil.checkMinChar(ownername, 3)) {
				error = true;
				inptCardOwnerName
						.setBackgroundResource(R.drawable.edittext_error);
			}

			if (error) {
				messageUtil
						.showToastMessage(getString(R.string.check_out_pay_error));
			} else {
				messageUtil
						.showToastMessage(getString(R.string.check_out_pay_success));
			}
		}
	};

	public void selectTab(int index) {
		selectedTab = index;
		tabLeft.setTextColor(Color.WHITE);
		tabMiddle.setTextColor(Color.WHITE);
		tabRight.setTextColor(Color.WHITE);

		tabLeft.setBackgroundResource(R.drawable.middle);
		tabMiddle.setBackgroundResource(R.drawable.middle);
		tabRight.setBackgroundResource(R.drawable.middle);

		if (index == 3) {
			tabRight.setBackgroundResource(R.drawable.middle_grey);
			tabRight.setTextColor(Color.BLACK);
		} else if (index == 2) {
			tabMiddle.setBackgroundResource(R.drawable.middle_grey);
			tabMiddle.setTextColor(Color.BLACK);
		} else {
			tabLeft.setBackgroundResource(R.drawable.middle_grey);
			tabLeft.setTextColor(Color.BLACK);
		}
	}
}
